from common.base import *


def get_double(dm,hwnd):
    # 1.获取设备编号
    number = get_number_from_title(hwnd)
    # 2.首页-挂机

    sleep(5)
    pic_path = dm.Capture(406,3,501,77, f"test{number}.bmp")
    retx,rety = OCR_get_text(pic_path,"挂|机")
    if retx != -1:
        retx = retx+406
        rety = rety+3
        print([retx, rety])
        dm.LeftClick(retx,rety,10,10)
    else:
        retx, rety = dm.Find_pic(406,3,501,77, "首页-挂机.bmp", "000000", 0.8, 0)
        if retx != -1 and rety != -1:
            dm.LeftClick(retx, rety, 10, 10)
        else:
            log.info(f"设备{number}，未找到，首页-挂机,点击坐标")
            dm.LeftClick(456,37, 10, 10)




    # 2.挂机-领取
    sleep(5)
    pic_path = dm.Capture(776,576,941,682, f"test{number}.bmp")
    retx,rety = OCR_get_text(pic_path,"领|取")
    if retx != -1:
        retx = retx+776
        rety = rety+576
        print([retx,rety])
        dm.LeftClick(retx,rety,10,10)
        sleep(2)
        dm.LeftClick(retx, rety, 10, 10)
    else:
        log.info(f"设备{number}，未找到，挂机-领取")
    # 已经领双，弹窗处理
    pic_path = dm.Capture(334,387,493,478, f"test{number}.bmp")
    retx, rety = OCR_get_text(pic_path, "取消")
    if retx != -1:
        dm.LeftClick(retx+334, rety+387, 10, 10)
        sleep(2)
    # 5.挂机-关闭
    for i in range(5):
        sleep(5)
        x, y = dm.Find_pic(916,81,990,144, "挂机-关闭X.bmp", "000000", 0.8, 0)
        if x != -1 and y != -1:
            dm.LeftClick(x, y, 10, 10)
            sleep(2)
            break
        else:
            log.info(f"设备{number}，未找到，挂机-关闭X.bmp")